package com.protobuf;

import java.io.IOException;

import com.protobuf.model.UserProto;
import com.protobuf.model.UserProto.User;

public class Test {
	
	
	public static void main(String[] args) throws IOException {
		
		
        //模拟将对象转成byte[]，方便传输
        UserProto.User.Builder builder = UserProto.User.newBuilder();
        builder.setId(1);
        builder.setName("ant");
                
        builder.addPhone(UserProto.User.PhoneNumber.newBuilder().setNumber("15000000"));
        builder.addPhone(UserProto.User.PhoneNumber.newBuilder().setNumber("15111111"));
        
        UserProto.User user = builder.build();
        System.out.println("before :"+ user.toString());

        System.out.println("===========Person Byte==========");
        for(byte b : user.toByteArray()){
            System.out.print(b);
        }
        System.out.println();
        System.out.println(user.toByteString());
        System.out.println("================================");

        //模拟接收Byte[]，反序列化成Person类
        byte[] byteArray =user.toByteArray();
        User p2 = user.parseFrom(byteArray);
        System.out.println("after :" +p2.toString());
    }

}
